package com.yanqu.road.server.cmd.activity.rankunionwar.cross;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.rankunionwar.config.RankUnionWarConfig;
import com.yanqu.road.entity.activity.rankunionwar.data.RankUnionWarGuessValueArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.RankUnionWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.rankunionwar.RankUnionWarMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Cmd(code = GameProtocol.S_RANKUNIONWAR_CROSS_CONDITION_SYNC, desc = "")
public class RankUnionWarCrossConditionSyncCmd extends AbstractCommand {

    RankUnionWarProto.RankUnionWarCrossConditionSyncMsg reqMsg;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = RankUnionWarProto.RankUnionWarCrossConditionSyncMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        RankUnionWarConfig config = RankUnionWarMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo()) || config.getActivityInfo().getActivityId() != reqMsg.getActivityId()) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        for(RankUnionWarProto.RankUnionWarCrossConditionMsg crossSyncMsg : reqMsg.getSyncDataList()){
            if(crossSyncMsg.getUserId() == -1 && crossSyncMsg.getType() == 4){
                //触发所有在线玩家，更新竞猜猜中人数到param
                List<GamePlayer> onlinePlayerList = GamePlayerMgr.getAllOnlinePlayer();
                for(GamePlayer onlinePlayer : onlinePlayerList){
                    onlinePlayer.notifyListener(eGamePlayerEventType.RankUnionWarMatchGuess.getValue(),
                            new RankUnionWarGuessValueArgs(new BigInteger(crossSyncMsg.getValue()),crossSyncMsg.getParam(),0));
                }
            }
            GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(crossSyncMsg.getUserId());
            if(gamePlayer != null){
                //1门客击杀人数,2进入赛场,3积分排行4竞猜
                if(crossSyncMsg.getType() == 1){
                    gamePlayer.notifyListener(eGamePlayerEventType.RankUnionWarKillCount.getValue(), new BigInteger(crossSyncMsg.getValue()));
                }else if(crossSyncMsg.getType() == 2){
                    gamePlayer.notifyListener(eGamePlayerEventType.RankUnionWarDivision.getValue(),new BigInteger(crossSyncMsg.getValue()));
                }else if(crossSyncMsg.getType() == 3){//这个没用，没有个人榜单
                    gamePlayer.notifyListener(eGamePlayerEventType.RankUnionWarUnionScoreRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL,new BigInteger(crossSyncMsg.getValue()), player.getUserInfo().getUnionUid()));
                }else if(crossSyncMsg.getType() == 4){
                    Map<Integer,Integer> guessNumMap = new ConcurrentHashMap<>();
                    List<String> tempStringList = StringUtils.stringToStringList(crossSyncMsg.getParam(),"\\|");
                    BigInteger turn = new BigInteger(tempStringList.get(0));
                    //判断最高的那个去触发
                    List<ActivityConditionInfo> conditionInfoList = NormalActivityMgr.getActivityConditionInfoList(config.getActivityInfo().getActivityId());
                    for(ActivityConditionInfo activityConditionInfo : conditionInfoList){
                        List<BigInteger> paramList = activityConditionInfo.getParamList();
                        if(paramList.get(2).equals(turn)){
                            guessNumMap.put(activityConditionInfo.getConditionId(),paramList.get(3).intValue());
                        }
                    }
                    int maxGuessCount = 0;
                    int maxConditionId = 0;
                    int rightCount = Integer.valueOf(crossSyncMsg.getValue());
                    for(int conditionId : guessNumMap.keySet()){
                        int guessCount = guessNumMap.get(conditionId);
                        if(guessCount >= maxGuessCount && rightCount >= guessCount){
                            maxGuessCount = guessCount;
                            maxConditionId = conditionId;
                        }
                    }
                    gamePlayer.notifyListener(eGamePlayerEventType.RankUnionWarMatchGuess.getValue(),
                            new RankUnionWarGuessValueArgs(new BigInteger(crossSyncMsg.getValue()),crossSyncMsg.getParam(),maxConditionId));
                }else if(crossSyncMsg.getType() == 5){
                    gamePlayer.notifyListener(eGamePlayerEventType.RankUnionWarWinTimes.getValue(), new BigInteger(crossSyncMsg.getValue()));
                }
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
    }
}
